package web.Courts;

import com.google.gson.Gson;
import entity.CourtsBean;
import services.CourtsServiceAll;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getCourtsByInfo")
public class GetCourtsByInfo extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过球桌信息查询
        resp.setContentType("application/json");
        String searchName = req.getParameter("searchName");
        String selectTypeValue = req.getParameter("selectTypeValue");
        String selectStateValue = req.getParameter("selectStateValue");
        List<CourtsBean> list = null;
        CourtsServiceAll courtsServiceAll = new CourtsServiceAll();
        if ("".equals(searchName)) {
            if ("所有".equals(selectTypeValue)) {
                if ("所有".equals(selectStateValue)) {
                    list = courtsServiceAll.getCourtsAll();
                } else{
                    list = courtsServiceAll.getCourtsByStatus(selectStateValue);
                }
            } else if ("中8台球".equals(selectTypeValue)) {
                if ("所有".equals(selectStateValue)) {
                    list = courtsServiceAll.getCourtsByType(selectTypeValue);
                } else{
                    list = courtsServiceAll.getCourtsByTypeStaus(selectStateValue,selectTypeValue);
                }
            } else if ("斯诺克".equals(selectTypeValue)) {
                if ("所有".equals(selectStateValue)) {
                    list = courtsServiceAll.getCourtsByType(selectTypeValue);
                } else{
                    list = courtsServiceAll.getCourtsByTypeStaus(selectStateValue,selectTypeValue);
                }
            }
        } else {
            if ("所有".equals(selectTypeValue)) {
                if ("所有".equals(selectStateValue)) {
                    System.out.println("通过名字查询");
                    list = courtsServiceAll.getCourtsByName(searchName);
                } else {
                    list = courtsServiceAll.getCourtsByNameStaus(searchName,selectStateValue);
                }
            } else if ("中8台球".equals(selectTypeValue)) {
                if ("所有".equals(selectStateValue)) {
                    list = courtsServiceAll.getCourtsByNameType(searchName,selectTypeValue);
                } else {
                    list = courtsServiceAll.getCourtsByNameTypeStaus(searchName,selectTypeValue,selectStateValue);
                }
            } else if ("斯诺克".equals(selectTypeValue)) {
                if ("所有".equals(selectStateValue)) {
                    list = courtsServiceAll.getCourtsByNameType(searchName,selectTypeValue);
                } else {
                    list = courtsServiceAll.getCourtsByNameTypeStaus(searchName,selectTypeValue,selectStateValue);
                }
            }
        }
        //asd
        Gson gson = new Gson();
        String json = gson.toJson(list);
        resp.getWriter().println(json);
    }
}
